const {dialog,MenuItem,app,shell} = require('electron')
const fs = require('fs')
const path = require("path")
const os_1 = require('os')
const publish = require('../../app-menu-publish')
const dicts = require('../../common/app-string')
const configPath = path.join(os_1.homedir(), app.name, 'blog_templates', 'config.json');
const DataStore = require('../../app-store');
const dataStore = new DataStore();
const appLogin = require('../../cookie/app-login');
const toast = require('../../common/app-toast')
const string = require('../../common/app-string');
const exec = require('child_process').exec
const appMenuPublish = require('../../app-menu-publish');

exports.menu = function (menu, tray) {
    const options = {
        label: '批量推送博客',
        click: function () {
            dialog.showOpenDialog({
                properties: ['openDirectory', 'createDirectory']
            })
            .then(files => {
                if (!files.canceled) {
                    const markdownSelects = []
                    const selectDirectory = files.filePaths[0]
                    options.readDirectory(selectDirectory, markdownSelects)
                    let markdownFiles = {
                        canceled: false,
                        files:[]
                    }
                    markdownSelects.forEach(function(filepath,index,array){
                        let infos = filepath.split("/")
                        let title = infos[infos.length-2]
                        let dirname = path.dirname(filepath)
                        let content = fs.readFileSync(filepath)
                        let extname = path.extname(filepath)
                        markdownFiles.files.push({
                            filepath: filepath,
                            title: title,
                            content: content.toString(),
                            dirname: dirname,
                            extname: extname
                        })
                    })
                    publish.publishArticleTo(tray, dicts.solo, false, 1000, markdownFiles)
                }
            })
            .catch(err => {
                console.log(err)
            })
        },
        readDirectory(filePath, markdowns=[]){
            //fs.readdir读取文件目录
            fs.readdirSync(filePath).forEach((item,index)=>{
                let data = fs.statSync(path.join(filePath, item))
                if(data.isDirectory()){
                    this.readDirectory(path.join(filePath, item),markdowns)
                }else if(data.isFile() && item.endsWith(".md")){
                    markdowns.push(path.join(filePath, item))
                }
            })
        }
    }
    menu.append(new MenuItem({
        label: '登录个人博客',
        click: function(){
            appLogin.loginSolo()
        }
    }))
    menu.append(new MenuItem({
        label: '单篇推送博客',
        click: function(){
            appMenuPublish.publishArticleTo(tray, string.solo, false, 5000,{})
        }
    }))
    menu.append(new MenuItem(options))
    let data = fs.readFileSync(configPath)
    const params = JSON.parse(data.toString());
    let subMenu = []
    for (const param of params.template) {
        subMenu.push(new MenuItem({
            id: param.id,
            label: param.label,
            type: 'radio',
            checked: dataStore.isOpenTemplate(param.id),
            click: function(menuItem){
                menuItem.checked = true
                dataStore.setTemplate(param)
            }
        }))
    }
    menu.append(new MenuItem({
        label: '更新图片资源',
        click: function (){
            let message = exec("./publish.sh",{cwd: require('path').join(require('os').homedir(), app.name,'bin')})
            toast.toast({"title":"图片更新","body":`${message}`})
        }
    }))
    menu.append(new MenuItem({
        label: '博客站点配置',
        click: function (){
            shell.openItem(configPath)
        }
    }))
    menu.append(new MenuItem({label:"使用模板", submenu: subMenu}))
    tray.setTitle('插件')
}
